/*
   Copyright 2008, Mark Arvidson

   This file is part of EwaScorecard.

   EwaScorecard is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   EwaScorecard is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with EwaScorecard.  If not, see <http://www.gnu.org/licenses/>.
   */

import java.io.*;

public class GymnastData {

    private String name;
    private int number;
    private float[] scores;

    /**
     * Create a new gymnast
     * @param number Gymnast Number
     * @param name Gymnast Name
     * @param scores Array of scores
     */
    public GymnastData(int number, String name, float[] scores) {
	this.number = number;
	this.name = name;
	this.scores = scores;
    }

    /**
     * Hash of gymnast
     * @return integer hash code
     */
    public int hashCode() {
	return getNumber() + getName().hashCode();
    }

    /**
     * Compare gymnasts by number and name
     * @param candidate 
     * @return true if the gymnasts are the same
     */
    public boolean equals(Object candidate) {
	GymnastData that = (GymnastData) candidate;
	return that != null &&
	    this.getNumber() == that.getNumber() &&
	    this.getName().equals(that.getName());
    }

    /**
     * @return gymnast number-name string
     */
    public String toString() {
	if (getNumber() > 0) {
	    return getNumber() + "-" + getName();
	}

	return getName();
    }

    /**
     * Set the score of the given event to the given value.
     * @param index event index
     * @param score floating point score value
     */
    public void setScore(int index, float score) {
	// don't limit AllArond scores
	if (index < scores.length - 1) {
	    while (score > 20) {
		score /= 10;
	    }
	}
	if (score > 0) {
	    if (index >= 0 && index < scores.length) {
		scores[index] = score;
	    }
	}
    }

    public void storeRecord(DataOutputStream stream, boolean storeAsDefault) {
	try {
	    stream.writeUTF(getName());
	    stream.writeInt(getNumber());
	    stream.writeInt(getScores().length);
	    for (int i = 0; i < getScores().length; i++) {
		stream.writeFloat(storeAsDefault ? 0.0f : getScores()[i]);
	    }
	} catch (Exception e) {
	    System.out.println(e.getMessage());
	    e.printStackTrace();
	}
    }

    public static GymnastData restoreRecord(DataInputStream stream) {
	try {
	    String name = stream.readUTF();
	    int number = stream.readInt();
	    int length = stream.readInt();
	    float[] scores = new float[length];
	    for (int i = 0; i < length; i++) {
		scores[i] = stream.readFloat();
	    }
	    return new GymnastData(number, name, scores);
	} catch (Exception e) {
	    System.out.println(e.getMessage());
	    e.printStackTrace();
	}
	return null;
    }

    public String getName() {
	return name;
    }

    public int getNumber() {
	return number;
    }

    public float[] getScores() {
	return scores;
    }
}

